package com.pmt.feedplayer.ui

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.pmt.feedplayer.controller.TranslationController

/**
 * 字幕显示组件
 * 显示实时翻译的字幕内容
 */
@Composable
fun SubtitleView(
    translationController: TranslationController,
    modifier: Modifier = Modifier,
    textSize: Float = 16f,
    backgroundColor: Color = Color.Black.copy(alpha = 0.5f),
    textColor: Color = Color.White,
    padding: Dp = 8.dp,
    showBackground: Boolean = true
) {
    val currentSubtitle by translationController.currentSubtitle.collectAsState()
    val isTranslationEnabled by translationController.isTranslationEnabled.collectAsState()
    
    // 只有当翻译功能启用且有字幕内容时才显示
    AnimatedVisibility(
        visible = isTranslationEnabled && currentSubtitle.isNotEmpty(),
        enter = fadeIn(),
        exit = fadeOut()
    ) {
        Box(
            modifier = modifier
                .fillMaxWidth()
                .wrapContentHeight()
                .then(if (showBackground) {
                    Modifier.background(backgroundColor)
                } else {
                    Modifier
                })
                .padding(padding)
        ) {
            Text(
                text = currentSubtitle,
                style = TextStyle(
                    color = textColor,
                    fontSize = textSize.sp,
                    fontWeight = FontWeight.Normal,
                    textAlign = TextAlign.Center,
                    lineHeight = (textSize * 1.4).sp
                ),
                modifier = Modifier.align(Alignment.Center)
            )
        }
    }
}

/**
 * 字幕历史记录组件
 * 显示最近的翻译记录
 */
@Composable
fun SubtitleHistoryView(
    translationController: TranslationController,
    modifier: Modifier = Modifier,
    maxItems: Int = 5,
    textSize: Float = 14f
) {
    val subtitleHistory by translationController.subtitleHistory.collectAsState()
    val isTranslationEnabled by translationController.isTranslationEnabled.collectAsState()
    
    if (isTranslationEnabled && subtitleHistory.isNotEmpty()) {
        Column(
            modifier = modifier
                .fillMaxWidth()
                .background(Color.Black.copy(alpha = 0.7f))
                .padding(12.dp)
        ) {
            Text(
                text = "翻译历史",
                color = Color.White,
                fontSize = (textSize * 0.9).sp,
                modifier = Modifier.padding(bottom = 8.dp)
            )
            
            subtitleHistory.takeLast(maxItems).forEachIndexed { index, result ->
                Column(modifier = Modifier.padding(bottom = 8.dp)) {
                    Text(
                        text = result.originalText, // 原始文本
                        color = Color.Gray,
                        fontSize = (textSize * 0.8).sp,
                        maxLines = 1
                    )
                    Text(
                        text = result.translatedText, // 翻译后文本
                        color = Color.White,
                        fontSize = textSize.sp,
                        maxLines = 2
                    )
                }
            }
        }
    }
}

/**
 * 翻译控制面板
 * 提供翻译功能的开关和设置
 */
@Composable
fun TranslationControlPanel(
    translationController: TranslationController,
    modifier: Modifier = Modifier,
    onLanguageChange: () -> Unit = {}
) {
    val isTranslationEnabled by translationController.isTranslationEnabled.collectAsState()
    val translationState by translationController.translationState.collectAsState()
    
    Row(
        modifier = modifier
            .background(Color.Black.copy(alpha = 0.8f))
            .padding(12.dp)
            .fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Switch(
            checked = isTranslationEnabled,
            onCheckedChange = { translationController.toggleTranslation() },
            colors = SwitchDefaults.colors(
                checkedThumbColor = if (translationState == com.pmt.feedplayer.data.TranslationState.ERROR) {
                    Color.Red
                } else {
                    Color.Green
                }
            )
        )
        
        Column(modifier = Modifier.padding(horizontal = 12.dp)) {
            Text(
                text = "实时翻译",
                color = Color.White,
                fontSize = 16.sp,
                fontWeight = FontWeight.Medium
            )
            Text(
                text = getStateText(translationState),
                color = Color.Gray,
                fontSize = 12.sp
            )
        }
        
        Button(
            onClick = onLanguageChange,
            colors = ButtonDefaults.buttonColors(
                containerColor = Color.DarkGray
            )
        ) {
            Text(text = "语言设置", color = Color.White, fontSize = 14.sp)
        }
    }
}

private fun getStateText(state: com.pmt.feedplayer.data.TranslationState): String {
    return when (state) {
        com.pmt.feedplayer.data.TranslationState.IDLE -> "未开始"
        com.pmt.feedplayer.data.TranslationState.STARTING -> "启动中..."
        com.pmt.feedplayer.data.TranslationState.TRANSLATING -> "翻译中"
        com.pmt.feedplayer.data.TranslationState.PAUSED -> "已暂停"
        com.pmt.feedplayer.data.TranslationState.STOPPED -> "已停止"
        com.pmt.feedplayer.data.TranslationState.ERROR -> "错误"
        else -> "未知状态"
    }
}